/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.dialogs;

import com.google.inject.Inject;
import listo.client.ImageProvider;
import listo.client.MainForm;
import listo.client.WindowState;
import listo.client.dialogs.controllers.*;
import listo.utils.reflection.Reflector;
import listo.utils.swing.CollapsiblePanel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class AddTaskDialog extends TaskDialog {

    private JButton buttonOkAndNew;
    private boolean okAndNewPressed;

    @Inject
    public AddTaskDialog(MainForm mainForm, ImageProvider imageProvider, Reflector reflector, WindowState windowState,
                         TaskQuickEntryController quickEntryController, TaskDescController descController,
                         TaskDueDateController dueDateController, TaskDueTimeController dueTimeController,
                         TaskReminderController reminderController, TaskFoldersController foldersController,
                         TaskClearTextController clearTextController, TaskNoteController noteController) {
        super(mainForm, imageProvider, reflector, windowState, quickEntryController, descController, dueDateController,
                dueTimeController, reminderController, foldersController, clearTextController, noteController);
    }

    protected void createUI() {
        buttonOkAndNew = new JButton("Ok and New");
        ActionListener buttonOkAndNewActionListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                okAndNewPressed = true;
                onOk();
            }
        };
        buttonOkAndNew.addActionListener(buttonOkAndNewActionListener);
        buttonOkAndNew.registerKeyboardAction(buttonOkAndNewActionListener,
                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);

        super.createUI();
    }

    protected void createLayout(CollapsiblePanel noteEntryPanel, JPanel clearTextPanel, int maxWidth) {
        layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING).
                        addComponent(quickEntryPanel, GroupLayout.DEFAULT_SIZE, windowState.getTaskDialogWidth(),
                                maxWidth).
                        addComponent(standardEntryPanel).
                        addComponent(noteEntryPanel).
                        addComponent(clearTextPanel).
                        addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().
                        addComponent(buttonOk).
                        addComponent(buttonOkAndNew).
                        addComponent(buttonCancel)));
        layout.setVerticalGroup(
                layout.createSequentialGroup().
                        addComponent(quickEntryPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                GroupLayout.PREFERRED_SIZE).
                        addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).
                        addComponent(standardEntryPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                GroupLayout.PREFERRED_SIZE).
                        addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).
                        addComponent(noteEntryPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE, 400).
                        addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).
                        addComponent(clearTextPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                GroupLayout.PREFERRED_SIZE).
                        addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).
                        addGroup(layout.createParallelGroup().
                        addComponent(buttonOk).
                        addComponent(buttonOkAndNew).
                        addComponent(buttonCancel)));

        layout.linkSize(SwingConstants.HORIZONTAL, buttonOk, buttonOkAndNew, buttonCancel);
    }

    protected void setButtonOKEnabled(boolean enabled) {
        super.setButtonOKEnabled(enabled);
        buttonOkAndNew.setEnabled(enabled);
    }

    public boolean isOkAndNewPressed() {
        return okAndNewPressed;
    }

}